// 删除用户信息
function removeUser(id){
    let reqData = new Map()
    reqData['ids'] = [id]
    $.ajax({
        url: '/admin/user/delete',
        type: 'POST',
        data: JSON.stringify(reqData),
        dataType: 'JSON',
        contentType: 'application/json;charset=utf-8',
        error: (err) => {
            console.error(err.responseText)
            open_toast('warning', '删除用户信息请求提交失败.')
        },
        success: (data) => {
            open_toast(data['code'] === 1 ? 'success' : 'fail', data['response'])
        }
    })
}
// 删除多个用户信息
function removeUsers(){
    let reqData = new Map()
    // 获取当前选中的用户
    let users = []
    // 获取选中的所有用户code
    $('.cb_user').each(function(){
        if($(this).prop('checked') === true)
            users.push($(this).data('id'))
    })
    reqData['ids'] = users
    $.ajax({
        url: '/admin/user/delete',
        type: 'POST',
        data: JSON.stringify(reqData),
        contentType: 'application/json;charset=utf-8',
        dataType: 'JSON',
        error: (err) => {
            console.error(err.responseText)
            open_toast('warning', '删除用户信息请求提交失败.')
        },
        success: (data) => {
            open_toast(data['code'] === 1 ? 'success' : 'fail', data['response'])
        }
    })
}
// 动态渲染用户信息
function loadUserInfo(userInfo){
    $('#userTbody').html('')
    $.each(userInfo, function (i, user) {
        var date = new Date(user['birthday']);
        $('#userTbody').append(`<tr valign="baseline">
<td scope="row"><input class="cb_user" data-id=${user['id']} type="checkbox"></td>
<td>${user['id']}</td>
<td>${user['code']}</td>
<td>${user['username']}</td>
<td>${user['gender'] === 1 ? '女' : '男'}</td>
<td>${date.getFullYear()}-${date.getMonth()}-${date.getDay()}</td>
<td>${user['phone']}</td>
<td>${user['address']}</td>
<td>
       <img width="60px" height="60px" style="border-radius: 50%" src="${user['img']}">
</td>
<td>
<nav>
<button class="btn btn-primary btn-sm btn-modal-update"
                            data-bs-toggle="modal" data-bs-target="#div-modal" onclick="getUserInfoByCode('${user['code']}')">编辑</button>
<button class="btn btn-danger btn-sm" onclick="removeUser('${user['id']}')">删除</button>
</nav>
</td>
</tr>`)
    });
}

// 动态渲染分页信息
function loadPageInfo(pageInfo) {
    $('#div-admin-user-page').html('')

    // 首先设置每一页的HTML
    let $perPageInfo = $('<li></li>')
    for (let i in `${pageInfo.navigatepageNums}`) {
        if(i === '0' || i > pageInfo.pages) continue
        $perPageInfo.append(`
        <li>
            <a
                href="${updateCurrentUrlParam('pageNow', i)}" 
                class="page-link d-block text-dark ${i == pageInfo.pageNum ? ' bg-primary' : ''}">
                ${i}
            </a>
        </li>
        `)
    }
    // 如果页数大于1在进行渲染
    if(pageInfo.pages > 1){
        $('#div-admin-user-page').html(`
     <!-- 分页部分 -->
    <h1 class="u-text-sm text-center">第${pageInfo.pageNum}页 共${pageInfo.pages}页</h1>
    <nav aria-label="Page navigation" class="u-text-sm">
        <ul class="pagination justify-content-center">
            <!--首页-->
            <li class="page-item">
                <a class="page-link d-block text-dark" href="${updateCurrentUrlParam('pageNow', pageInfo.navigateFirstPage)}">
                    首页
                </a>
            </li>
            <!--上一页-->
            <li class="page-item">
                <a class="page-link d-block text-dark"
                   href="${updateCurrentUrlParam('pageNow', pageInfo.prePage)}" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>`
            + $perPageInfo.html() +
            `
            <!--下一页-->
            <li class="page-item">
                <a class="page-link d-block text-dark"
                   href="${updateCurrentUrlParam('pageNow', pageInfo.nextPage)}" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
            <!--最后一页-->
            <li class="page-item">
                <a class="page-link d-block text-dark"
                   href="${updateCurrentUrlParam('pageNow', pageInfo.navigateLastPage)}">
                    末页
                </a>
            </li>
        </ul>
    </nav>`)
    }
}
// 动态渲染标签信息
function loadSearchTag() {
    let map = getUrlParamToMap();
    if(map.length > 0){
        map.forEach((index, k) => {
            if (k !== 'pageNow' && searchTypeArray.indexOf(decodeURI(k)) !== -1) {
                ualert(decodeURI(k), decodeURI(map.get(k)), 'success')
            }
        })
    }
    let orderById = getUrlParam(document.location.href, 'orderById');
    if(orderById === 'down') {
        $('#btn-order').html(`<i class="bi bi-caret-down-square"></i>`)
            .addClass('down')
            .attr('onclick', `location = updateCurrentUrlParam("orderById", "up")`)

    } else{
        $('#btn-order').html(`<i class="bi bi-caret-up-square-fill"></i>`)
            .addClass('up')
            .attr('onclick', `location = updateCurrentUrlParam("orderById", "down")`)
    }

}